package com.webex.ta.hydra.function;

import com.webex.ta.hydra.core.engine.HydraContext;
import com.webex.ta.hydra.core.engine.HydraContextService;
import com.webex.ta.hydra.core.engine.HydraVariables;
import com.webex.ta.hydra.exceptions.InvalidVariableException;

import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: vegaz
 * Date: 2010-8-30
 * Time: 13:47:20
 * To change this template use File | Settings | File Templates.
 */
public class SimpleVariable {
    private String name;

	public SimpleVariable(String name) {
		this.name = name;
	}

	public SimpleVariable() {
		this.name = "";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString() {
		String ret = null;
		HydraVariables vars = getVariables();
        String vname = null;
        try {
            vname = VariableReplacer.transform(name);
        } catch (InvalidVariableException e) {
            e.printStackTrace();
        }

        if (vars != null) {
			ret = vars.getStringValue(vname);
		}

        if (ret == null) {
			return "${" + name + "}";
		}

		return ret;
	}

	private HydraVariables getVariables() {
		HydraContext context = HydraContextService.getContext();
		return context.getVariables();
	}
}
